<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>消费管理</title>
    <style>
        .import {
            margin-bottom: 10px;
        }

        .line {
            display: inline-block;
            vertical-align: top;
        }
    </style>
</head>
<body>
<div>

</div>
<table id="table" class="display">
    <thead>
    <tr>
        {{--        <th>ID</th>--}}
        <th>商户名称</th>
        <th>商户号</th>
        <th>消费类型</th>
        <th>消费金额</th>
        <th>消费时间</th>
    </tr>
    </thead>
    <tbody>
    </tbody>
    <tfoot></tfoot>
</table>
</body>
<script>
    var _dataTablesInstance
    //jQuery的载入事件
    $(function () {
        $('#table').dataTable().fnDestroy();
        //初始化datatables
        _dataTablesInstance = $('#table').dataTable({
            //开启服务端模式
            serverSide: true,
            //指定服务端数据获取地址（ajax地址）
            ajax: "/admin/consume/list",
            //将数据表的列与表格的列进行绑定
            columns: [
                // {"data": "id"},
                {"data": "merchant"},
                {"data": "tenant_id"},
                {"data": "consume_type"},
                {"data": "consume_fee"},
                {"data": "created_at"},
            ],
            //createdRow回调去完善每一行
            createdRow: function (row, data, dataIndex) {
                //给每一行添加text-c的class属性值，让其居中
                // $(row).addClass('text-c');
                //需要更改第一个单元格的内容为复选框
                // $(row).find('td:eq(0)').html("<input type='checkbox' class='patch_del' data-mobile='"+ data.mobile +"' value='" + data.id + "'/>");
                // $(row).find('td:eq(3)').html("<span type='checkbox' class='mobile'>"+data.mobile+"</span>");

                // var button = "<button onclick=edit('"+data.tenant_id+"') type='button' class='layui-btn layui-btn-xs'>编辑</button>";
                // button += "<button onclick=charge('"+data.tenant_id+"') type='button' class='layui-btn layui-btn-xs'>手充</button>";
                // button += "<button onclick=deductfee('"+data.tenant_id+"') type='button' class='layui-btn layui-btn-xs'>扣费</button>";
                //
                // //判断帐号状态，根据对应值显示对应的效果
                // $(row).find('td:eq(5)').html(button);
            },
            //部分字段不参与排序
            // aoColumnDefs: [ { "bSortable": false, "aTargets": [ 0,2,3,4,5,6 ] }] ,
            //更改默认排序
            // aaSorting: [[ 1, "desc" ]],
            // bSort: false,
            language: {
                "sProcessing": "处理中...",
                "sLengthMenu": "显示 _MENU_ 项结果",
                "sZeroRecords": "没有匹配结果",
                "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
                "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                "sInfoPostFix": "",
                "sSearch": "搜索:",
                "sUrl": "",
                "sEmptyTable": "暂无数据",
                "sLoadingRecords": "载入中...",
                "sInfoThousands": ",",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "上页",
                    "sNext": "下页",
                    "sLast": "末页"
                },
                "oAria": {
                    "sSortAscending": ": 以升序排列此列",
                    "sSortDescending": ": 以降序排列此列"
                },
                "bPaginate": true, //翻页功能
            }
        });
    });
</script>
<script>
    //编辑
    function edit(tenant_id) {
        layer.open({
            title: '编辑',
            type: 2,
            content: '/admin/adminuser/edit?tenant_id='+tenant_id,
            area: ['600px', '300px'],
            btn: ['确定', '取消'],
            yes:function (index,layero) {
                var body = layer.getChildFrame('body', index); //得到iframe页的body内容
                var username = body.find('input[name=username]').val();
                var merchant = body.find('input[name=merchant]').val();
                $.ajax({
                    headers: {
                        'X-CSRF-TOKEN': "{{ csrf_token() }}"
                    },
                    url: '/admin/adminuser/edit',
                    type: 'post',
                    data: {
                        tenant_id: tenant_id,
                        merchant: merchant,
                        username: username,
                    },
                    success: function (res) {
                        layer.msg(res.text);
                        if(res.code == 0){
                            layer.close(index);
                            _dataTablesInstance.fnDraw(false);
                        }
                    }
                });
            }
        });
    }
    //扣费设置
    function deductfee(tenant_id) {
        layer.open({
            title: '扣费设置',
            type: 2,
            content: '/admin/adminuser/deductfee?tenant_id='+tenant_id,
            area: ['600px', '600px'],
            btn: ['确定', '取消'],
            yes:function (index,layero) {
                var body = layer.getChildFrame('body', index); //得到iframe页的body内容
                var message_fee = body.find('input[name=message_fee]').val();
                var buer_fee = body.find('input[name=buer_fee]').val();
                var idcard_fee = body.find('input[name=idcard_fee]').val();
                var alive_fee = body.find('input[name=alive_fee]').val();
                var carrier_fee = body.find('input[name=carrier_fee]').val();
                $.ajax({
                    headers: {
                        'X-CSRF-TOKEN': "{{ csrf_token() }}"
                    },
                    url: '/admin/adminuser/deductfee',
                    type: 'post',
                    data: {
                        tenant_id: tenant_id,
                        message_fee: message_fee,
                        buer_fee: buer_fee,
                        idcard_fee: idcard_fee,
                        alive_fee: alive_fee,
                        carrier_fee: carrier_fee,
                    },
                    success: function (res) {
                        layer.msg(res.text);
                        if(res.code == 0){
                            layer.close(index);
                            _dataTablesInstance.fnDraw(false);
                        }
                    }
                });
            }
        });
    }
    //充值
    function charge(tenant_id) {
        layer.open({
            title: '编辑',
            type: 2,
            content: '/admin/adminuser/charge?tenant_id='+tenant_id,
            area: ['600px', '200px'],
            btn: ['确定', '取消'],
            yes:function (index,layero) {
                var body = layer.getChildFrame('body', index); //得到iframe页的body内容
                var cash = body.find('input[name=cash]').val();
                $.ajax({
                    headers: {
                        'X-CSRF-TOKEN': "{{ csrf_token() }}"
                    },
                    url: '/admin/adminuser/charge',
                    type: 'post',
                    data: {
                        tenant_id: tenant_id,
                        cash: cash,
                    },
                    success: function (res) {
                        layer.msg(res.text);
                        if(res.code == 0){
                            layer.close(index);
                            _dataTablesInstance.fnDraw(false);
                        }
                    }
                });
            }
        });
    }

    function del(oid, mobile) {
        layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
            $.ajax({
                url: '/admin/common/black/delblack',
                type: 'post',
                data: {
                    'oid': oid,
                    'mobile': mobile
                },
                success: function (res) {
                    console.log();
                    if (res.code == 0) {
                        layer.msg(res.text)
                        $(".container-refresh").click();
                        layer.close(index);
                    } else {
                        layer.msg(res.text);
                    }
                }
            })
        });
    }

    $(function () {
        $('.patch_btn').click(function () {
            var vals = $('.patch_del:checked').val();
            if (typeof (vals) == 'undefined') {
                layer.msg('没有选择数据');
                return;
            }
            var orderid = [];
            var mobiles = [];
            $('.patch_del:checked').each(function (index, ele) {
                orderid.push($(ele).val());
                mobiles.push($(ele).attr('data-mobile'));
            });
            if (count(orderid) <= 0) {
                layer.msg('没有选择数据');
                return;
            }
            var orderids = orderid.join(',');
            var mobiles = mobiles.join(',');
            layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                $.ajax({
                    url: '/admin/common/black/delblack',
                    type: 'post',
                    data: {
                        'oid': orderids,
                        'mobile': mobiles
                    },
                    success: function (res) {
                        if (res.code == 0) {
                            layer.msg(res.text)
                            $(".container-refresh").click();
                            layer.close(index);
                        } else {
                            layer.msg(res.text);
                        }
                    }
                })
            });
        })

        $('.import').click(function (merchant) {
            layer.open({
                title: '添加商户',
                type: 2,
                content: '/admin/adminuser/add?merchant='+merchant,
                area: ['600px', '400px'],
                btn: ['确定', '取消'],
                yes:function (index,layero) {
                    var body = layer.getChildFrame('body', index); //得到iframe页的body内容
                    var password = body.find('input[name=password]').val();
                    var username = body.find('input[name=username]').val();
                    var merchant = body.find('input[name=merchant]').val();

                    if(!username){
                        layer.msg('用户名不能为空');
                        return;
                    }
                    if(!password){
                        layer.msg('密码不能为空');
                        return;
                    }
                    $.ajax({
                        headers: {
                            'X-CSRF-TOKEN': "{{ csrf_token() }}"
                        },
                        url: '/admin/adminuser/add',
                        type: 'post',
                        data: {
                            merchant: merchant,
                            username: username,
                            password: password,
                        },
                        success: function (res) {
                            layer.msg(res.text);
                            if(res.code == 0){
                                layer.close(index);
                                _dataTablesInstance.fnDraw(false);
                            }
                        }
                    });
                }
            });
        });
    })
</script>
<script>
    function count(o) {//获取数组长度
        var t = typeof o;
        if (t == 'string') {
            return o.length;
        } else if (t == 'object') {
            var n = 0;
            for (var i in o) {
                n++;
            }
            return n;
        }
        return false;
    }
</script>
</html>